﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Diagnostics;
using System.Threading;
using System.Reflection;

namespace SuraQR.Controller
{
    class QRCodeReaderService
    {
        private string COM_PORT_NAME = "COM5";
        private bool isStart = false;
        private IQRCodeReaderListenner listener;
        private SerialPort mySerialPort = null;




        public bool IsStart
        {
            get { return isStart; }
            set { isStart = value; }
        }
        public string COM_PORT_NAME1
        {
            get { return COM_PORT_NAME; }
            set { COM_PORT_NAME = value; }
        }


        public void init()
        {
                mySerialPort = new SerialPort(COM_PORT_NAME1);
                mySerialPort.BaudRate = 9600;
                mySerialPort.Parity = Parity.None;
                mySerialPort.StopBits = StopBits.One;
                mySerialPort.DataBits = 8;
                mySerialPort.Handshake = Handshake.None;
        }

        
        public void start(){
            try
            {

                if (IsStart == false)
                {
                    Debug.WriteLine("Start ......................." + COM_PORT_NAME1);
                    mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                    IsStart = true;
                    mySerialPort.Open();

                }

            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Data);
                Debug.WriteLine("End Process..................");
                IsStart = false;

                if (listener != null)
                {
                    listener.onError(e);
                }
            }
        }

        public void StartTread()
        {
            if (IsStart == false)
            {
                ThreadStart th = new ThreadStart(this.start);
                Thread childThread = new Thread(th);
                childThread.Name = "QR CODE THREAD";
                childThread.Start();
            }
        }

        public void close()
        {
            if (IsStart)
            {
                mySerialPort.Close();
                IsStart = false;
                Debug.WriteLine("Close.................");
            }
        }

        private void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
        {
            try
            {
                SerialPort sp = (SerialPort)sender;
                string indata = sp.ReadExisting();
                Debug.WriteLine("Data Received :" + indata);
                if (listener != null)
                {
                    listener.readDataQR(indata);
                }
            
            }catch (Exception ex)
            {
                Debug.WriteLine(ex);
                IsStart = false;
                if (mySerialPort.IsOpen)
                {
                    mySerialPort.Close();
                }
            }
            
        }


        public void setListener(IQRCodeReaderListenner listener)
        {
            this.listener = listener;
        }
    }

        
}
